package com.cn.tous.auth.config;


import com.cn.tous.auth.oauth2.serializer.OAuth2AuthorizationDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.jackson2.CoreJackson2Module;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationServerJackson2Module;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

/**
 * @author mengwei
 * @description WebConfig
 * @createDate 2025/7/30 9:17
 */
@Configuration
public class WebConfig {

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder
                // 设置连接超时时间（毫秒）
                .connectTimeout(Duration.ofSeconds(5))
                // 设置读取超时时间（毫秒）
                .readTimeout(Duration.ofSeconds(5))
                .build();
    }
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());

        // 使用GenericJackson2JsonRedisSerializer来序列化和反序列化redis的value值
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.afterPropertiesSet();
        return template;
    }

// Authorization 序列化配置
//    @JsonSerialize(using = OAuth2AuthorizationSerializer.class)
    @JsonDeserialize(using = OAuth2AuthorizationDeserializer.class)
    abstract class OAuth2AuthorizationMixin {}
    @Bean
    public ObjectMapper objectMapper(){

        ObjectMapper objectMapper = new ObjectMapper()
                .registerModule(new JavaTimeModule()) // 支持 Instant 等时间类型
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 日期格式化为字符串
        objectMapper.registerModule(new CoreJackson2Module())  // 支持 Authentication
                .registerModule(new OAuth2AuthorizationServerJackson2Module());

        objectMapper.addMixIn(OAuth2Authorization.class, OAuth2AuthorizationMixin.class);

        return  objectMapper;
    }


}
